package com.bauer.base.io;

import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author ：rui.wei
 * @Date ：Created in 9:43 2019/6/20
 * @Description：
 */
public class NioDemo {

    public static void main(String[] args) throws Exception {
        String basePath = BuffedInputStreamDemo.class.getResource("/").getPath();
        RandomAccessFile file = new RandomAccessFile(basePath + "inputtext.txt", "rw");
        FileInputStream fileInputStream = new FileInputStream(basePath + "inputtext.txt");
        FileChannel fileChannel = file.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        int len = -1;
        while ((len = fileChannel.read(byteBuffer)) != -1) {
            byteBuffer.flip();

            while (byteBuffer.hasRemaining()) {
                System.out.print((char) byteBuffer.get());
            }
            byteBuffer.clear();
        }
        fileChannel.close();
        file.close();

    }
}
